import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/login',
    component: () => import('@/views/hy/Login.vue')
  },
  {
    path: '/reset-password',
    component: () => import('@/views/hy/ResetPassword.vue')
  },
  {
    path: '/dashboard',
    component: () => import('@/views/hy/Dashboard.vue'),
    children: [
      // 团长管理
      {
        path: '/leader/audit',
        component: () => import('@/views/hy/leader/LeaderAudit.vue')
      },
      {
        path: '/leader/management',
        component: () => import('@/views/hy/leader/LeaderManagement.vue')
      },
      {
        path: '/leader/tags',
        component: () => import('@/views/hy/leader/LeaderTags.vue')
      },
      // 内容管理
      {
        path: '/content/advertisement',
        component: () => import('@/views/hy/content/Advertisement.vue')
      },
      {
        path: '/content/platform-materials',
        component: () => import('@/views/hy/content/Platform.vue')
      },
      {
        path: '/content/hot-words',
        component: () => import('@/views/hy/content/Hotwords.vue')
      },
      // 系统管理
      {
        path: '/system/account',
        component: () => import('@/views/hy/UserManagement.vue')
      },
      {
        path: '/system/role',
        component: () => import('@/views/hy/RoleManagement.vue')
      },
      {
        path: '/system/permission',
        component: () => import('@/views/hy/PermissionManagement.vue')
      },
      // 用户管理
      {
        path: '/user/management',
        component: () => import('@/views/hy/user/UserManagement.vue')
      },
      {
        path: '/user/tags',
        component: () => import('@/views/hy/user/UserTags.vue')
      },
      // 运营管理
      {
        path: '/operation/flash-sale',
        component: () => import('@/views/bkc/FlashSale.vue')
      },
      {
        path: '/operation/flash-sale/create',
        component: () => import('@/views/bkc/FlashSaleCreate.vue')
      },
      {
        path: '/operation/flash-sale/edit/:id',
        component: () => import('@/views/bkc/FlashSaleEdit.vue')
      },
      {
        path: '/operation/group-buy',
        component: () => import('@/views/bkc/GroupBuy.vue')
      },
      {
        path: '/operation/group-buy/edit/:id',
        component: () => import('@/views/bkc/GroupBuyEdit.vue')
      },
      {
        path: '/operation/coupon',
        component: () => import('@/views/bkc/Coupon.vue')
      },
      {
        path: '/operation/coupon/detail/:id',
        component: () => import('@/views/bkc/CouponDetail.vue')
      },
      {
        path: '/operation/coupon/edit/:id',
        component: () => import('@/views/bkc/CouponEdit.vue')
      },
      {
        path: '/operation/full-reduction',
        component: () => import('@/views/bkc/FullReduction.vue')
      },
      // 财务管理
      {
        path: '/finance/reconciliation',
        component: () => import('@/views/bkc/Reconciliation.vue')
      },
      {
        path: '/finance/commission',
        component: () => import('@/views/bkc/Commission.vue')
      },
      {
        path: '/finance/withdrawal',
        component: () => import('@/views/bkc/Withdrawal.vue')
      },
      {
        path: '/finance/order-detail/:id',
        component: () => import('@/views/bkc/OrderDetail.vue')
      },
      // 商品模块
      {
        path: '/product/management',
        component: () => import('../views/jjx/Management.vue')
      },
      {
        path: '/product/category',
        component: () => import('../views/jjx/Category.vue')
      },
      {
        path: '/product/inventory',
        component: () => import('../views/jjx/Inventory.vue')
      },
      // 订单模块
      {
        path: '/order/management',
        component: () => import('../views/dingdan/Management.vue')
      },
      {
        path: '/order/after-sales',
        component: () => import('../views/dingdan/After-sales.vue')
      },
      {
        path: '/order/shipping',
        component: () => import('../views/dingdan/Shipping.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, _from, next) => {
  document.title = to.meta?.title ? `${to.meta.title} - 中台管理系统` : '中台管理系统'
  const publicPages = ['/login', '/reset-password']
  const accessToken = localStorage.getItem('accessToken')
  if (!publicPages.includes(to.path) && !accessToken) {
    next('/login')
  } else {
    next()
  }
})

export default router 